/**
 * 
 */
package unseen.games.ui.manager;

import unseen.games.config.Config;
import unseen.games.taptap.MainActivity;
import unseen.games.taptap.R;
import unseen.games.ui.GenericScreenManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author PERRAUDIN Steven <sperraudin@neotilus.com>
 *
 * @date 18 juin 2010
 */
public class SettingsManager extends GenericScreenManager implements OnClickListener{
	
	private View mSettingsView;
	private Context mContext;
	
	private Button mResetHighScores;
	private TextView mChangeDifficulty, mDifficulty;
	
	private final CharSequence[] items = {"5 seconds", "10 seconds", "30 seconds"};
	
	public SettingsManager(Context context, View view) {
		super(context);

		mContext = context;
		
		initView(view);
	}

	private void initView(View view) {
		
		mSettingsView = view;
		
		mResetHighScores = (Button) mSettingsView.findViewById(R.id.reset_high_scores);
		mResetHighScores.setOnClickListener(this);
		
		mChangeDifficulty = (TextView)mSettingsView.findViewById(R.id.change_difficulty);
		mChangeDifficulty.setOnClickListener(this);
		
		mDifficulty	= (TextView)mSettingsView.findViewById(R.id.difficulty);
		mDifficulty.setText(Config.TIME_LIMIT + " seconds");
		mDifficulty.setOnClickListener(this);
	}

	private void changeDifficulty(){
		int checked = (Config.TIME_LIMIT == 5 ? 0 : (Config.TIME_LIMIT == 10 ? 1 : 2));
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("Pick a difficulty");
		builder.setSingleChoiceItems(items, checked, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		        if(item == 0){
		        	Config.TIME_LIMIT = 5;
		        }
		        else if(item == 1){
		        	Config.TIME_LIMIT = 10;
		        }
				else if(item == 2){
					Config.TIME_LIMIT = 30;
				}
		        mDifficulty.setText(Config.TIME_LIMIT + " seconds");
		        Toast mToast = Toast.makeText(mContext, mContext.getString(R.string.high_score_difficulty_changed, Config.TIME_LIMIT), Toast.LENGTH_SHORT);
				mToast.show();
				dialog.cancel();
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch(viewId)
		{
			case R.id.reset_high_scores:
				((MainActivity) mContext).resetScore();
				Toast mToast = Toast.makeText(mContext, mContext.getString(R.string.high_score_reset), Toast.LENGTH_SHORT);
				mToast.show();
				break;
			case R.id.change_difficulty:
				changeDifficulty();
				break;
			case R.id.difficulty:
				changeDifficulty();
				break;
		}
	}
}